package top.honlnk.czuan.service.impl.module.add;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.honlnk.czuan.common.constant.UpdateMorphemeCountConstant;
import top.honlnk.czuan.common.context.UserContextHolder;
import top.honlnk.czuan.mapper.AddMapper;
import top.honlnk.czuan.mapper.get.GetWordMapper;
import top.honlnk.czuan.mapper.update.UpdateWordRelationMapper;
import top.honlnk.czuan.pojo.entity.User;
import top.honlnk.czuan.pojo.entity.Words;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Component
public class AddRelationSIModule {
    @Autowired
    private AddMapper addMapper;

    @Autowired
    private UpdateWordRelationMapper updateWRMapper;

    @Autowired
    private GetWordMapper getWordMapper;

    @Autowired AddWordSIModule addWordSIModule;

    // 获取当前用户的账号
    private String getUserAccount() {
        User currentUser = UserContextHolder.getUser();
        return currentUser.getAccount();
    }

    public void addWordTORootRelation(Integer wordId, Integer rootId) {
        addMapper.addWordRootRelation(wordId, rootId);
        // 更新表示词根被继承的数量：+1
        addMapper.updateRootCount(rootId, UpdateMorphemeCountConstant.INCREMENT);

        // 更新单词表中的词根id以及词根翻译id列表
        Words word = getWordMapper.getWordById(wordId);
        List<Integer> rootIds = word.getRootsIds();
        rootIds.add(rootId);
        String tIds = addWordSIModule.getRTIdListJson(rootIds);
        updateWRMapper.updateRootRelation(wordId, rootId, tIds, getUserAccount());
    }
    public void addWordToPrefixRelation(Integer wordId, Integer prefixId) {
        addMapper.addWordPrefixRelation(wordId, prefixId);
        // 更新表示词前缀被继承的数量：+1
        addMapper.updatePrefixCount(prefixId, UpdateMorphemeCountConstant.INCREMENT);

        // 更新单词表中的前缀id以及前缀翻译id列表
        Words word = getWordMapper.getWordById(wordId);
        List<Integer> prefixIds = word.getPrefixesIds();
        prefixIds.add(prefixId);
        String tIds = addWordSIModule.getPFTIdListJson(prefixIds);
        updateWRMapper.updatePrefixRelation(wordId, prefixId, tIds, getUserAccount());

    }
    public void addWordToSuffixRelation(Integer wordId, Integer suffixId) {
        addMapper.addWordSuffixRelation(wordId, suffixId);
        // 更新表示词后缀被继承的数量：+1
        addMapper.updateSuffixCount(suffixId, UpdateMorphemeCountConstant.INCREMENT);

        // 更新单词表中的后缀id以及后缀翻译id列表
        Words words = getWordMapper.getWordById(wordId);
        List<Integer> suffixIds = words.getSuffixesIds();
        suffixIds.add(suffixId);
        String tIds = addWordSIModule.getSFTIdListJson(suffixIds);
        updateWRMapper.updateSuffixRelation(wordId, suffixId, tIds, getUserAccount());
    }

    public void addWordToTranslationRelation(Integer wordId, Integer translationId) {
        addMapper.addWordTranslationRelation(wordId, translationId);

        // 获取单词所有的翻译id列表
        Words words = getWordMapper.getWordById(wordId);
        JSONObject tIdsJson = words.getTIdsJson();
        List<Integer> allValueArray = new ArrayList<>();
        for (String key : tIdsJson.keySet()) {
            allValueArray.addAll(tIdsJson.getJSONArray(key).toList(Integer.class));
        }
        allValueArray.add(translationId);
        String tIds = addWordSIModule.getPartAndTIdListJson(allValueArray);
        updateWRMapper.updateTranslationRelation(wordId, tIds, getUserAccount());
    }

    public void addRootToTranslationRelation(Integer rootId, Integer translationId) {
        addMapper.addRootTranslationRelation(rootId, translationId);
    }
    public void addPrefixToTranslationRelation(Integer prefixId, Integer translationId) {
        addMapper.addPrefixTranslationRelation(prefixId, translationId);
    }
    public void addSuffixToTranslationRelation(Integer suffixId, Integer translationId) {
        addMapper.addSuffixTranslationRelation(suffixId, translationId);
    }
}
